﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
namespace QuanLyThi
{
    public partial class FrmDangKyLopHocTheoLop : Form
    {
        public static string mConnect = FrmMain.mConnect;
        private string msql = "";
        public DGC_Library mLibrary = new DGC_Library(mConnect);

        public FrmDangKyLopHocTheoLop()
        {
            InitializeComponent();
            this.Load();
            //comboBox1_SelectionChangeCommitted(null, null);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Load()
        {

            msql = "Select * from tb_CTdaotao order by CT_daotao";
            DataTable dt = mLibrary.GetDataTable(msql, "table");
            cchuongtrinhdt.DataSource = dt;
            cchuongtrinhdt.DisplayMember = "ct_daotao";
            cchuongtrinhdt.ValueMember = "MS_daotao";

            //DANH SACH HOC VIEN CHUA DANG KY
            msql = "Select a.ms_hssv,a.hovaten,a.gioitinh,a.namsinh,a.diachi,a.dienthoai,a.mail  from tb_hssv a left outer join tb_hssvlop b " +
                    " on a.ms_hssv=b.MS_hssv where b.ms_Lop is null Order by a.hovaten";
            DataTable dt2 = mLibrary.GetDataTable(msql, "tb1");
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = dt2;
           
        }

        private void cchuongtrinhdt_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select a.* from tb_Khoadaotao a " +
                  " inner join tb_cauhinhCtdaotao b " +
                  " on a.MS_Khoadaotao=b.ms_Khoadaotao " +
                  " Where b.ms_daotao="+cchuongtrinhdt.SelectedValue.ToString();
            DataTable dt1 = mLibrary.GetDataTable(msql, "table");
            ckhoa.DataSource = dt1;
            ckhoa.DisplayMember = "khoadaotao";
            ckhoa.ValueMember = "MS_khoadaotao";
        }

        private void ckhoa_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select * from tb_Lopdaotao Where MS_Khoadaotao="+ckhoa.SelectedValue.ToString();
            DataTable dt2 = mLibrary.GetDataTable(msql, "table");
            clop.DataSource = dt2;
            clop.DisplayMember = "tenlop";
            clop.ValueMember = "MS_lop";
        }

        private void clop_SelectionChangeCommitted(object sender, EventArgs e)
        {          
            string mslop = clop.SelectedValue.ToString();
            string mskhoa = ckhoa.SelectedValue.ToString();
            msql = "Select a.ms_hssv,a.hovaten,a.gioitinh,a.namsinh,a.diachi,a.dienthoai,a.mail from tb_hssv a " +
                   " inner join tb_hssvlop b on a.ms_hssv=b.ms_hssv " +
                   " where b.ms_lop='"+clop.SelectedValue.ToString()+"' Order by a.hovaten";
            DataTable dt1 = mLibrary.GetDataTable(msql, "tb1");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt1;           
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                string makhoa = ckhoa.SelectedValue.ToString();
                msql = "Select * from tb_Lopdaotao Where Ngaydangky<=getdate() " +
                        " and Getdate()<=Ngayketthuc and MS_Khoadaotao=" + makhoa;
                DataTable dt = mLibrary.GetDataTable(msql, "ASD");
                clop.DataSource = dt;
                clop.DisplayMember = "tenlop";
                clop.ValueMember = "ms_lop";
            }
            else
            {   
                string makhoa = ckhoa.SelectedValue.ToString();
                msql = " Select * from tb_Lopdaotao Where MS_Khoadaotao=" + makhoa;
                DataTable dt = mLibrary.GetDataTable(msql, "ASD");
                clop.DataSource = dt;
                clop.DisplayMember = "tenlop";
                clop.ValueMember = "ms_lop";
            }
        }



        private void butup_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView2.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView2.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView2.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView2.CurrentRow.Cells[2].Value.ToString();
                    newrow[3] = dataGridView2.CurrentRow.Cells[3].Value.ToString();
                    newrow[4] = dataGridView2.CurrentRow.Cells[4].Value.ToString();
                    newrow[5] = dataGridView2.CurrentRow.Cells[5].Value.ToString();
                    newrow[6] = dataGridView2.CurrentRow.Cells[6].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    DataGridViewRow row = dataGridView2.CurrentRow;
                    dataGridView2.Rows.Remove(row);
                    //DataTable dt2 = (DataTable)dataGridView2.DataSource;
                    //dt2.Rows[dataGridView2.CurrentRow.Index].Delete();


                    //dataGridView2.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butdown_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView2.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                    newrow[3] = dataGridView1.CurrentRow.Cells[3].Value.ToString();
                    newrow[4] = dataGridView1.CurrentRow.Cells[4].Value.ToString();
                    newrow[5] = dataGridView1.CurrentRow.Cells[5].Value.ToString();
                    newrow[6] = dataGridView1.CurrentRow.Cells[6].Value.ToString();
                    dt.Rows.Add(newrow);

                    //Delete row phia trên
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                    //DataTable dt2 = (DataTable)dataGridView1.DataSource;
                    //dt2.Rows[dataGridView1.CurrentRow.Index].Delete();
                    //dataGridView1.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private string LayMaHSSV()
        {
            string re = "'";
            DataTable dt = (DataTable)dataGridView1.DataSource;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    string Ma = "'";
                    if (dt.Rows[i]["MS_hssv"].ToString() == null)
                        Ma = "0";
                    else
                        Ma = dt.Rows[i]["MS_hssv"].ToString();
                    re = re + "','" + Ma;
                }
            }
            return re+"'";
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Bạn có muốn lưu thông tin này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes)
                {
                    string malop = clop.SelectedValue.ToString();                  
                    //cập nhật thông tin mới
                    string MaKhoaDT = LayMaHSSV();
                    msql = "Delete tb_hssvlop Where ms_lop='" + malop + "' and MS_hssv not in("+MaKhoaDT+")";
                    mLibrary.RunProc(msql);
                    MessageBox.Show("Lưu thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lưu thông tin không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }     
        }

        private void butLuu_Click_1(object sender, EventArgs e)
        {
            butLuu_Click(null, null);
        }

        private void sapxeptheoms_CheckedChanged(object sender, EventArgs e)
        {
            //THEO HOC VIEN MOI
            if (!sapxeptheoms.Checked)
            {
                msql = "Select a.ms_hssv,a.hovaten,a.gioitinh,a.namsinh,a.diachi,a.dienthoai,a.mail from tb_hssv a left outer join tb_hssvlop b " +
                        " on a.ms_hssv=b.MS_hssv where b.ms_Lop is null Order by a.hovaten";
                DataTable dt2 = mLibrary.GetDataTable(msql, "tb1");
                dataGridView2.AutoGenerateColumns = false;
                dataGridView2.DataSource = dt2;
            }
            else
            {
                //Load combo
                msql = "Select MS_lop, tenlop" +
                       "  from tb_Lopdaotao" +
                       " Order by Tenlop";
                DataTable dt = mLibrary.GetDataTable(msql, "table");
                comboBox1.DataSource = dt;
                comboBox1.DisplayMember = "tenlop";
                comboBox1.ValueMember = "MS_lop";  
                              
            }
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //Load sv
            msql = "Select a.ms_hssv,a.hovaten,a.gioitinh,a.namsinh,a.diachi,a.dienthoai,a.mail from tb_hssv a inner join tb_hssvlop b " +
                   " on a.ms_hssv=b.ms_hssv where b.ms_lop='" + comboBox1.SelectedValue.ToString() + "'" +
                   "  and a.ms_hssv not in ('''') Order by a.hovaten";
            DataTable dt3 = mLibrary.GetDataTable(msql, "tb1");
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = dt3;
        }          
     
    }
}
